import Eureka

//修改简历简历基本信息表单

class updateResumeBasicInfoController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    // 传过来的简历
    var resume:detailResume = detailResume()

	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< TextRow("resumeName"){ row in
                row.title = "简历名称"
                row.placeholder = "输入简历名称"
        }
        form +++ Section()
            <<< TextRow("userName"){ row in
                row.title = "用户名称"
                row.placeholder = "输入用户名"
            }
            <<< SegmentedRow<String>("gender") {
                $0.title = "性        别        "
                $0.options = ["男", "女"]
                $0.cell.segmentedControl.tintColor = UIColor.hexStringToColor("#ff4081")
            }
            <<< SegmentedRow<String>("marriage") {
                $0.title = "婚        姻        "
                $0.options = ["已婚", "未婚"]
                $0.cell.segmentedControl.tintColor = UIColor.hexStringToColor("#ff4081")
            }
            <<< DateRow("birthday"){
                $0.title = "生        日"
                $0.value = DateFormatter.dateFromString("2010-01-01")
            }
            <<< PushRow<String>("degree"){
                $0.title = "学        历"
                $0.selectorTitle = "选择你的学历"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_DEGREE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("workyear"){
                $0.title = "工作年限"
                $0.selectorTitle = "选择你的工作年限"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_WORKYEAR)
                $0.options = array
                $0.value = array[0]
            }
            <<< PhoneRow("mobile"){
                $0.title = "手机号码"
                $0.placeholder = "输入你的手机号码"
            }
            <<< EmailRow("email"){
                $0.title = "邮        箱"
                $0.placeholder = "输入你的邮箱"
            }
            <<< PushRow<String>("policticalstatus"){
                $0.title = "政治面貌"
                $0.selectorTitle = "选择你的政治面貌"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_POLICTICAL_STATUS)
                print("测试是否超出")
                print(enumUtil.RESUME_POLICTICAL_STATUS)
                print(array)
                $0.options = array
                $0.value = array[0]
            }
            <<< TextAreaRow("seleveluation"){
                $0.title = "自我评价"
                $0.placeholder = "输入你的自我评价"
            }
            +++ Section("现居地")
            <<< PushRow<String>("province"){
                $0.title = "省份"
                $0.selectorTitle = "省份"
                let array=enumUtil.getOrderedStringArray(enumUtil.PROVINCE)
                $0.options = array
                $0.value = ""
                
                $0.onChange{row in
                    // 初始化城市
                    print("你更改了省份")
                    let cityRow:PushRow<String>=self.form.rowByTag("city")!
                    print(row.value)
                    if(row.value != nil){
                        let provinceID=enumUtil.getKeyByString(row.value!, dict: enumUtil.PROVINCE)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.PROVINCE_CITYS[provinceID]!)
                        print(littleArray)
                        cityRow.options = littleArray
                        cityRow.value = littleArray[0]
                        cityRow.updateCell()
                    }
                }
            }
            <<< PushRow<String>("city"){
                $0.title = "市"
                $0.selectorTitle = "城市"
            }
            
        
            +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "保存"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })
        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "删除"
                row.cell.tintColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.delete()
                })


        loadData()
    }

    //  加载用户基本信息
    private func loadData(){

        // 等待提示
        waiting.show(self)
        
        if(resume.ResumeId != ""){
            
            var tr:TextRow
            var pr:PushRow<String>
            var dr:DateRow
            var tar:TextAreaRow
            var sgr:SegmentedRow<String>
            var phr:PhoneRow
            var emr:EmailRow
            
            tr=form.rowByTag("resumeName")!
            tr.value=resume.ResumeName
            tr=form.rowByTag("userName")!
            tr.value=resume.UserName
            tar=form.rowByTag("seleveluation")!
            tar.value=resume.SelfEvaluation
            sgr=form.rowByTag("gender")!
            sgr.value=resume.Gender
            sgr=form.rowByTag("marriage")!
            sgr.value=resume.Marriage
            pr=form.rowByTag("degree")!
            pr.value=resume.HighDegree
            pr=form.rowByTag("workyear")!
            pr.value=resume.WorkYears
            phr=form.rowByTag("mobile")!
            phr.value=resume.Mobile
            emr=form.rowByTag("email")!
            emr.value=resume.Email
            
            let cityCode:Int = Int(resume.xjdLocaltionCode)!
            let provinceCode:Int = enumUtil.getParentCodeByChildCode(cityCode)
            let provinceName=enumUtil.getStringByKey(provinceCode, dict: enumUtil.PROVINCE)
            let cityList = enumUtil.PROVINCE_CITYS[provinceCode]
            let cityName = enumUtil.getStringByKey(cityCode, dict: cityList!)
            
            pr=form.rowByTag("province")!
            pr.value=provinceName
            pr=form.rowByTag("city")!
            pr.options = enumUtil.getOrderedStringArray(cityList!)
            pr.value = cityName

            let DateFormatter=NSDateFormatter()
            DateFormatter.dateFormat="yyyy-M-d H:mm:ss"
            dr=form.rowByTag("birthday")!
            dr.value=DateFormatter.dateFromString(resume.Birthday)
        }
        
        waiting.stop()
    }
    

    private func submitForm(){
        // 等待提示
        waiting.show(self)
        //获取表单结果
        let dict = form.values()
        
        if(dict["province"] == nil || dict["city"] == nil){
            warningAlertUtil(title: "错误", msg: "未填写完整").show(self)
            return 
        }

        //获取用户名
//        let userId = userUtil.getCurrUserId()
        let resumeName = dict["resumeName"] as! String
        let userName = dict["userName"] as! String
        let gender = enumUtil.getKeyByString(dict["gender"] as! String,dict: enumUtil.RESUME_SEX)
        let marriage = enumUtil.getKeyByString(dict["marriage"] as! String,dict: enumUtil.RESUME_MARRIAGE)
        let birthday = dict["birthday"] as! NSDate
        let degree = enumUtil.getKeyByString(dict["degree"] as! String,dict: enumUtil.RESUME_DEGREE)
        //        let workyear = enumUtil.getKeyByString(dict["workyear"] as! String,dict: enumUtil.RESUME_WORKYEAR)
        let workyear = enumUtil.getKeyByString(dict["workyear"] as! String,dict: enumUtil.RESUME_WORKYEAR)
        let mobile = dict["mobile"] as! String
        let email = dict["email"] as! String
        let province = enumUtil.getKeyByString(dict["province"] as! String,dict: enumUtil.PROVINCE)
        let location = enumUtil.getKeyByString(dict["city"] as! String,dict: enumUtil.PROVINCE_CITYS[province]!)
        let policticalstatus = enumUtil.getKeyByString(dict["policticalstatus"] as! String,dict: enumUtil.RESUME_POLICTICAL_STATUS)
        let seleveluation = dict["seleveluation"] as! String
        
        resumeManager.UpdateResumeBasicInfo(resume.ResumeId,
                             resumeName:resumeName,
                             userName:userName,
                             gender:gender,
                             marriage:marriage,
                             birthday:birthday,
                             degree:degree,
                             location:location,
                             workyear:workyear,
                             mobile:mobile,
                             email:email,
                             policticalstatus:policticalstatus,
                             seleveluation:seleveluation,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }


    private func delete(){
        // 等待提示
        waiting.show(self)

        resumeManager.DeleteResumeById(resume.ResumeId,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


